import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:myflutterproject/customsinglechildlayout.dart';

class LoadingLayout extends CustomSingleChildLayout {
  LoadingLayout({key, isLoading})
      : super(
            key: key,
            delegate: _LoadingLayoutDelegate(),
            child: isLoading
                ? CircularProgressIndicator(
                    value: null,
                  )
                : Image.asset('images/index_01.png'));
}

class _LoadingLayoutDelegate extends SingleChildLayoutDelegate {
  @override
  bool shouldRelayout(covariant SingleChildLayoutDelegate oldDelegate) {
    //是否需要relayout
    return false;
  }

  @override
  BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
    var childWidth = min(constraints.maxWidth, constraints.maxHeight);
    var childBoxConstraints=BoxConstraints.tight(Size(childWidth/2,childWidth/2));
    return childBoxConstraints;
  }

  @override
  Offset getPositionForChild(Size size, Size childSize) {
    //确定child的位置，返回一个相对于parent的偏移值
    //size是layout的大小，由getSize确定
    //childSize由getConstraintsForChild得出的Constraints对child进行约束，得到child自身的size
   var dx=(size.width-childSize.width)/2;
   var dy=(size.height-childSize.height)/2;
    return Offset(dx, dy);
  }



}
